package com.library.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bean.Book;
import com.library.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Controller
public class BookController {

    @Resource
    private BookService bookService;

    /**
     * 获取书籍列表
     *
     * 此方法用于处理获取书籍列表的请求，可以根据书籍条件、开始日期和结束日期进行筛选
     * 同时，支持分页功能，默认情况下每页显示10条记录，从第1页开始
     *
     * @param book 书籍对象，包含可能的筛选条件
     * @param startDate 开始日期，用于筛选书籍
     * @param endDate 结束日期，用于筛选书籍
     * @param pageNum 当前页码，默认为1
     * @param pageSize 每页记录数，默认为10
     * @return 返回包含分页信息的书籍列表
     */
    @GetMapping("/books")
    @ResponseBody
    public Object getAllBooks(Book book,
                              String startDate,
                              String endDate,
                              @RequestParam(required = false,defaultValue = "1") int pageNum,
                              @RequestParam(required = false,defaultValue = "10") int pageSize){

        // 启用分页功能，设置当前页和每页大小
        PageHelper.startPage(pageNum,pageSize);

        // 根据条件筛选书籍，并执行分页查询
        Page<Book> page = (Page<Book>)bookService.selectBooksByCondition(book,startDate,endDate);

        // 将分页查询结果转换为PageInfo对象并返回
        return page.toPageInfo();
    }

    @RequestMapping("/deleteBook")
    @ResponseBody
    public String deleteBook(int bookId) {
        Book book = bookService.selectBookById(bookId);
       int result = bookService.deleteBook(bookId);
        if (result == 1) {
            String visitPath = book.getBookCover();
            String realPath = visitPath.replaceAll("/Library-Web/", "d:/");
            if (!realPath.equals("d:/bookImgs/default.png")) {
                new File(realPath).delete();
            }
        }
        return result == 1 ? "success" : "fail";

    }

    @RequestMapping("/book")
    public String updateBook(MultipartFile UpdateAvater,Book book) {

        if(UpdateAvater != null && !UpdateAvater.isEmpty() && UpdateAvater.getContentType().contains("image")){
            try {
                String fileName = UpdateAvater.getOriginalFilename();//获取文件原始文件名
                String newName = System.currentTimeMillis()+fileName;
                String savePath = "d:/bookImgs/"+newName;//确定 文件存储的路径
                String visitPath = "/Library-Web/bookImgs/"+newName;
                UpdateAvater.transferTo(new File(savePath));
                bookService.updateBookCover(book.getBookName(),visitPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        bookService.updateBook(book);
        return "redirect:/books.jsp";
    }

    @PostMapping("/addBook")
    public String addBook(MultipartFile avatar,Book book, Model model) {
        if (bookService.insertBook(book)==-1) {
            model.addAttribute("msg", "书名已存在");
            return "forward:addBook.jsp";
        }
        if(avatar != null && !avatar.isEmpty() && avatar.getContentType().contains("image")){
            try {
                String fileName = avatar.getOriginalFilename();//获取文件原始文件名
                String newName = System.currentTimeMillis()+fileName;
                String savePath = "d:/bookImgs/"+newName;//确定 文件存储的路径
                String visitPath = "/Library-Web/bookImgs/"+newName;
                avatar.transferTo(new File(savePath));
               bookService.updateBookCover(book.getBookName(),visitPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "redirect:/books.jsp";
    }

}
